/*
 *  WsvJdbc - a JDBC driver for WSV (values separated by whatever) data files
 *  Copyright (C) 2004  Daniele Pes
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package axs.jdbc.dataSpecification;

import axs.jdbc.utils.StringUtilities;


/**
 * This class is used to specify the pseudo-type of a fake column, that of a COUNT sql clause.
 * 
 * @author 		Daniele Pes
 * @version    $Id: SqlCounterFakeType.java,v 1.2 2007/04/16 17:24:37 montag451 Exp $
 */
public class SqlCounterFakeType {
	private static final String EMPTY_STATEMENT = "";
	private static final String ALL_RECORDS	  = "*";
	private static final String COUNT           = "COUNT";

	private String sqlItem = null;

	
	public SqlCounterFakeType(String item){
		sqlItem = item;
	}


	/**
	 * If columnNames == null then just sintactical correctness is evaluated. Otherwise, the item to counted 
	 * is searched for inside the array of names of columns.
	 * @param  sqlItem	  The SQL statement.
	 * @param  columnNames The array holding the names of columns.
	 * @return null if it's not a valid sql statement including a COUNT(whatever) clause. Otherwise, 
	 * 		  the item to be counted is returned.
	 */
	public static String getItemToBeCounted(String   sqlItem,
								  				       String[] columnNames){
		String result;
		//
		result = null;
		if(sqlItem != null && !sqlItem.equals(EMPTY_STATEMENT)){
			String upperNoSpaceSqlItem, itemToBeCounted;
			//
		   // this is because the counter could be specified, e.g., like " COUNT(*)" or " CoUnT( *          ) " or whatever	
			upperNoSpaceSqlItem = StringUtilities.getUpperCaseWithoutBlanks(sqlItem);		
			if(StringUtilities.isInString(upperNoSpaceSqlItem, COUNT)){
				// is a counter
				itemToBeCounted = extractItemToBeCounted(upperNoSpaceSqlItem); 
				if(itemToBeCounted!=null){
					if(columnNames == null)
						result = itemToBeCounted; // note: this can be not inclued in the set of names of columns
					else{
						if(itemToBeCounted.equals(ALL_RECORDS))
							result = itemToBeCounted;
						else
							// looking for in the columns names container
							if(StringUtilities.isInStringVector(columnNames, itemToBeCounted)){
								result = itemToBeCounted;
							}
							else
								; // result == null
					}
				}
			}
		}
		return result;		
	}
	
	
	public static boolean isAnItemToBeCounted(String   sqlItem,
														   String[] columnNames){
		if(getItemToBeCounted(sqlItem, columnNames) != null){
			return true;
		}
		else{
			return false;
		}
	}
		

	private static String extractItemToBeCounted(String upperNoSpaceSqlItem) {
		String result;
		int    countPos, argumentStartingPos, argumentEndingPos;
		//
		result 				  = null;		
		countPos            = upperNoSpaceSqlItem.indexOf(COUNT);
		argumentStartingPos = countPos + COUNT.length() + 1;
		argumentEndingPos   = upperNoSpaceSqlItem.indexOf(")", argumentStartingPos);
		if(argumentStartingPos>-1 && argumentEndingPos>-1){ 
			// has an argument
			if(argumentEndingPos-argumentStartingPos>0){
				// has a well defined argument
				result = upperNoSpaceSqlItem.substring(argumentStartingPos, argumentEndingPos);						
			}						
		} 
		return result;
	}
	

	public String getSqlItem() {
		return sqlItem;
	}

	
	public void setSqlItem(String item) {
		sqlItem = item;
	}

}
